/**
 * weapp-js v2.2.3(2022-4-26)
 * https://codeup.aliyun.com/motigo/motigo/weapp_base
 * Copyright@ 2022 Motigo, Inc.
 * 
 */
import BaseRequest from './base_request.js';
import Callback from './utils/callback.js';

class Payment extends Callback {
    constructor() {
        super();
        this.ERR_CANCEL = -100;
        this.ERR_UNKNOWN = -101;
    }

    createOrder(opts) {
        this._createOrder(
            BaseRequest.appid,
            BaseRequest.openid,
            opts.title,
            opts.price,
            opts.detail
        )
            .then(res => {
                this.success(opts, res);
            })
            .catch(err => {
                this.fail(opts, err);
            });
    }

    submitPayment(opts) {
        let orderInfo = opts.orderInfo;
        wx.requestPayment({
            timeStamp: orderInfo.payArgs.timestamp + '',
            nonceStr: orderInfo.payArgs.noncestr,
            package: orderInfo.payArgs.package,
            signType: 'MD5',
            paySign: orderInfo.payArgs.paySign,
            success() {
                this.success(opts, {
                    orderId: orderInfo.orderId
                });
            },
            fail: res => {
                let ret = -1;
                let msg = '';
                if (res.errMsg.indexOf('cancel') > -1) {
                    ret = this.ERR_CANCEL;
                    msg = 'canceled';
                }
                this.fail(opts, {
                    ret: ret,
                    msg: msg
                });
            }
        });
    }
    pay(opts) {
        let { appid = BaseRequest.appid, openid = BaseRequest.openid, title, price, detail } = opts;

        this._createOrder(appid, openid, title, price, detail).then(
            res => {
                if (res && res.pay_args) {
                    this.submitPayment(res)
                        .then(res => {
                            this.success(opts, res);
                        })
                        .catch(err => {
                            this.fail(opts, err);
                        });
                } else {
                    this.fail(opts, {
                        ret: this.ERR_UNKNOWN,
                        msg: '获取支付参数失败!'
                    });
                }
            },
            err => {
                this.fail(opts, err);
            }
        );
    }

    _createOrder(appid, openid, title, price, detail) {
        return new Promise((resolve, reject) => {
            BaseRequest.post(`/payment/order/${appid}`, {
                openid: openid,
                title: title,
                price: price,
                detail: detail
            }).then(data => {
                if (data && data.pay_args) {
                    resolve({
                        orderId: data.order_id,
                        payArgs: data.pay_args
                    });
                } else {
                    reject({
                        msg: '创建微信支付订单失败!'
                    });
                }
            }, reject);
        });
    }
}

export default new Payment();
